<?php

namespace User;

use C\L\WebUserController;

class BankController extends WebUserController
{
    protected function init()
    {
        $this->hideKeys = [
        'is_delete'
        ];
        $this->service = $this->s_bank;
    }

    public function beforeSearch()
    {
        $this->params['data']['uid'] = $this->uid;
        $this->params['page_num'] = 500;
        return true;
    }

    public function afterSearch(&$data)
    {
        $config = $this->s_bank->getStatusConfig();
        foreach ($data['list'] as &$item) {
            $item['username'] = $this->s_user->search(['uid' => $item['uid']])['name'];
            $item['scard'] = substr($item['card'], 0, 4) . '********' . substr($item['card'], -4, 4);
            if ($item['type'] == 'B') {
                $item['card'] = '尾号' . substr($item['card'], -4, 4) . '储蓄卡';
            } elseif ($item['type'] == 'A') {
                $item['card'] = '尾号' . substr($item['card'], -4, 4) . '支付宝号';
            }
            unset($item['uid']);
        }
        $data['config'] = $config;
        return true;
    }

  //设置默认银行卡
    public function setdefAction()
    {
        $uid = $this->uid;
        $id = $this->getValue('id', true);
        $bank = $this->s_bank->search($id);
        if (empty($bank) || $bank['uid'] != $uid) {
            $this->success();
        }

        if ($this->s_bank->setDefault($uid, $id)) {
            $this->success();
        }

        $this->error();
    }

  //添加银行卡
    public function addAction()
    {
        $uid = $this->uid;
        $name = $this->getValue('name', true);
        $card = $this->getValue('card', true);
        $type = $this->getValue('type', true);
        $addr = $this->getValue('addr', false);
        $is_default = $this->getValue('is_default', false);
        if (!in_array($type, ['A', 'B'])) {
            $this->error('type类型错误');
        }

      //        $banks = $this->s_bank->getStatusConfig()['code'];
      //        if(empty($banks[$name])){
      //            $this->error('当前银行不存在');
      //        }

        if ($this->s_bank->search(['card' => $card])) {
            $this->error('已存在');
        }

        $add = [
        'uid' => $uid,
        'type' => $type,
        'card' => $card,
        'code' => 'none',
        'name' => $name,
        'is_default' => $is_default ?? 'N'
        ];

        if ($this->s_bank->save($add)) {
            $this->success();
        }

        $this->error('添加失败');
    }

    public function indexAction()
    {
        $this->checkAuth();
        $type = $this->getValue('type') ?? 'B';
        $bank = $this->s_bank->search(['uid' => $this->uid, 'type' => $type], [], ['name', 'card', 'addr', 'id', 'type']);
        $this->success([
        'is_bind' => false,
        'bank' => array_merge($bank, [
        'username' => $this->s_user->getValue('name', $this->uid)
        ]),
        'config' => $this->s_bank->getStatusConfig(),
        ]);
    }

    public function applyAction()
    {
        $type = $this->getValue('type') ?? 'B';
      //        if ($this->s_bank->getCount(['uid' => $this->uid]) > 0) {
      //            $this->error('如需修改银行卡信息，请联系客服修改');
      //        }

        $name = $this->getValue('name', true, 'string');
        $card = $this->getValue('card', true, 'string');
        $addr = $this->getValue('addr', false, 'string');
        $id = $this->getValue('id', true, 'int');

        $add = [
        'uid' => $this->uid,
        'card' => $card,
        'code' => 'none',
        'name' => $name,
        'addr' => $addr,
        'type' => $type,
        ];

        if ($id) {
            $back = $this->s_bank->search($id);
            if (!$back) {
                $this->error('未知错误');
            }
            $this->s_bank->update($back['id'], $add);
            $this->success();
        } elseif ($this->s_bank->save($add)) {
            $this->success();
        }

        $this->error();
    }
}
